﻿using DTOs;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain;
using RBAC.Domain.Service.Impl;
using RBAC.Domain.Service.Interfae;
using RBACDuYaJun.Api.Appliction.Command.MenuCommand;
using RBACDuYaJun.Api.Dtos;
using static Azure.Core.HttpHeader;

namespace RBACDuYaJun.Api.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
   // [Authorize]//（JWT）
    [ApiController]
    public class MenusController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMenuService _menuService;
        public MenusController(IMediator mediator, IMenuService menuService)
        {
            _mediator = mediator;
            _menuService = menuService;
        }
        /// <summary>
        /// 显示菜单（根据用户ID查菜单）（利用Mediator）
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<MenuDTOs>>> GetMenusUserId([FromQuery]MenuShowCommand command)
        {
            ApiResult<PageDto<MenuDTOs>> result = new ApiResult<PageDto<MenuDTOs>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
            // return await _menuService.GetMenusUserId(uid, pageSize, pageIndex);
        }
        /// <summary>
        /// 添加菜单（利用Mediator）
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddMenu(MenuCreateCommand command)
        {
            return await _mediator.Send(command);
        }
        ///// <summary>
        ///// 添加菜单
        ///// </summary>
        ///// <param name="menu"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<int> AddMenu(Menu menu)
        //{
        //    return await _menuService.AddMenu(menu);
        //}
        /// <summary>
        /// 显示全部菜单（利用Mediator）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Menu>> GetMenuList()
        {
            return await _mediator.Send(new MenuListCommand());
        }
        /// <summary>
        /// 反填菜单（利用Mediator）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Menu>> GetMenuValue([FromQuery]MenuGetIdCommand command)
        {
            ApiResult<Menu> result = new ApiResult<Menu>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
            //return await _mediator.Send<Menu>(command);
            //return await _menuService.GetMenuValue(id);
        }
        /// <summary>
        /// 修改菜单(利用Mediator）
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateMenu(MenuUpdateCommand command)
        {
            return await _mediator.Send(command);
        }
        ///// <summary>
        ///// 显示（利用Mediator）
        ///// </summary>
        ///// <param name="pid"></param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<List<MenuDTOs>> GetMenus()
        //{
        //    return await _mediator.Send(new MenuShowCommand());
        //}
        ///// <summary>
        ///// 显示
        ///// </summary>
        ///// <param name="pid"></param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<List<MenuDTOs>> GetMenus()
        //{
        //   return await _menuService.GetMenus();
        //}
        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="Pid"></param>
        ///// <returns></returns>
        //[HttpDelete]
        //public async Task<int> DelMenu(int ids)
        //{
        //    return await _menuService.DelMenu(ids);
        //}
        /// <summary>
        /// 删除判断是否有子集
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DelMenu13(int ids)
        {
           return await _menuService.DelMenu13(ids);
        }
       
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> DelMenu2(int ids)
        {
            return await _menuService.DelMenu2(ids);
        }

    }
}
